<template>
  <div class="detail">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录帐户设置">
          <el-form
            ref="form"
            label-width="100px"
            class="form"
            :model="form"
            :rules="rules"
          >
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" class="input" />
            </el-form-item>
            <el-form-item label="新密码" prop="password">
              <el-input v-model="form.password" class="input" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" lazy
          ><Info :user-info="userInfo" @getData="getData"
        /></el-tab-pane>
        <el-tab-pane label="岗位详情" lazy>
          <Job />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { sysUser } from '@/api/user'
import { sysUserPut } from '@/api/employees'
// import Info from './components/Info.vue'
export default {
  //   components: { Info },
  //   异步组件
  components: {
    Info: () => import('./components/Info.vue'),
    Job: () => import('./components/Job.vue')
  },
  data () {
    return {
      userId: this.$route.params.id,
      initUserinfo: {},
      userInfo: {},
      form: {
        username: '', //	string	非必须
        password: '' //	string	非必须
      },
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 2, max: 16, message: '请输入2-16位姓名', trigger: 'change' }
        ], //	string	非必须
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 16, message: '请输入6-16位密码', trigger: 'change' }
        ] //	string	非必须
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysUser(this.userId)
      this.userInfo = JSON.parse(JSON.stringify(res.data))
      this.initUserinfo = res.data
      this.form.username = res.data.username
      console.log('员工详情', res)
    },
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          console.log('验证成功')
          await sysUserPut({ ...this.initUserinfo, ...this.form })
          this.$message.success('更新成功')

          // 更新其它使用用户信息数据的地方
          //   this.userInfo = { ...this.initUserinfo, ...this.form }
          this.getData()
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.detail {
  padding: 30px;
  .form {
    margin-left: 100px;
    margin-top: 40px;
    .input {
      width: 280px;
    }
  }
}
</style>
